package net.adst.beans;

import net.adst.util.MyUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * User: alekseipot
 * Date: 05.11.12
 * Time: 9:57
 * To change this template use File | Settings | File Templates.
 */
public class NaemTec {
    private Date date;
    private String chIN;
    private String chOut;
    private String tecName;
    private String managerName;
    private String customer;
    private String priceHoursCust;
    private String hoursCust;
    private String income;
    private String performer;
    private String priceHoursOur;
    private String hoursOur;
    private String amountOur;
    private String profit;
    private String objectName;
    private String vozvrat;
    private String comment;
    private Integer rowInFile;

    public NaemTec(HSSFRow row) {
        if (row.getCell(0).toString() != null) {
            this.date = row.getCell(0).getDateCellValue();
        } else {
            this.date = new Date();
        }
        if (row.getCell(1).toString() != null) {
            this.chIN = row.getCell(1).toString();
        } else {
            this.chIN = "";
        }
        if (row.getCell(2).toString() != null) {
            this.chOut = row.getCell(2).toString();
        } else {
            this.chOut = "";
        }

        if (row.getCell(3).toString() != null) {
            this.tecName = row.getCell(3).toString();
        } else {
            this.tecName = "";
        }
        if (row.getCell(4).toString() != null) {
            this.managerName = row.getCell(4).toString();
        } else {
            this.managerName = "";
        }
        if (row.getCell(5).toString() != null) {
            this.customer = row.getCell(5).toString();
        } else {
            this.customer = "";
        }
        if (row.getCell(6).toString() != null) {
            this.priceHoursCust = row.getCell(6).toString();
        } else {
            this.priceHoursCust = "";
        }
        if (row.getCell(7).toString() != null) {
            this.hoursCust = row.getCell(7).toString();
        } else {
            this.hoursCust = "";
        }
        if ((row.getCell(6).toString() != null) && (row.getCell(7).toString() != null) && (!row.getCell(6).toString().isEmpty()) && (!row.getCell(7).toString().isEmpty())) {
            Double priceHour = new Double(row.getCell(6).toString());
            Double hour = new Double(row.getCell(7).toString());
            this.income = String.valueOf(priceHour * hour);
        } else {
            this.income = "";
        }
        if (row.getCell(9).toString() != null) {
            this.performer = row.getCell(9).toString();
        } else {
            this.performer = "";
        }
        if (row.getCell(10).toString() != null) {
            this.priceHoursOur = row.getCell(10).toString();
        } else {
            this.priceHoursOur = "";
        }
        if (row.getCell(11).toString() != null) {
            this.hoursOur = row.getCell(11).toString();
        } else {
            this.hoursOur = "";
        }
        if ((row.getCell(10).toString() != null) && (row.getCell(11).toString() != null) && (!row.getCell(10).toString().isEmpty()) && (!row.getCell(11).toString().isEmpty())) {
            Double priceHour = new Double(row.getCell(10).toString());
            Double hour = new Double(row.getCell(11).toString());
            this.amountOur = String.valueOf(priceHour * hour);
        } else {
            this.amountOur = "";
        }
        if (row.getCell(14).toString() != null) {
            this.objectName = row.getCell(14).toString();
        } else {
            this.objectName = "";
        }
        if (row.getCell(15).toString() != null) {
            if (!row.getCell(15).toString().contains("*")) {
                this.vozvrat = row.getCell(15).toString();
            } else {
                String value = row.getCell(15).toString();
                this.vozvrat = String.valueOf(Double.valueOf(row.getCell(MyUtils.getAbcNum(value.toCharArray()[0])).toString()) * Double.valueOf((value.substring(value.lastIndexOf('*') + 1))));
            }
        } else {
            this.vozvrat = "";
        }
        if ((!income.equals("")) && (!amountOur.equals(""))) {
            if (!vozvrat.isEmpty()) {
                this.profit = String.valueOf(Double.valueOf(income) - Double.valueOf(amountOur) - Double.valueOf(row.getCell(11).toString()) * 60);
            } else {
                this.profit = String.valueOf(Double.valueOf(income) - Double.valueOf(amountOur));
            }
        } else {
            this.profit = "";
        }

        if (row.getCell(16) != null || row.getCell(17) != null || row.getCell(18) != null || row.getCell(19) != null || row.getCell(20) != null || row.getCell(21) != null || row.getCell(22) != null || row.getCell(23) != null || row.getCell(24) != null) {
            this.comment = row.getCell(16) != null ? row.getCell(16).toString() : "";
            for (int i = 17; i < 25; i++) {
                if (row.getCell(i) != null && !row.getCell(i).toString().equals("")) {
                    this.comment = this.comment.equals("") ? this.comment : this.comment + "\n" + row.getCell(i).toString();
                }

            }
            if (this.comment == null) {
                this.comment = "";
            }
        } else {
            this.comment = "";
        }
        this.rowInFile = row.getRowNum();
    }

    public NaemTec(Date date, String chIN, String chOut, String tecName, String managerName, String customer, String priceHoursCust, String hoursCust, String income, String performer, String priceHoursOur, String hoursOur, String amountOur, String profit, String objectName, String vozvrat) {
        this.date = date;
        this.chIN = chIN;
        this.chOut = chOut;
        this.tecName = tecName;
        this.managerName = managerName;
        this.customer = customer;
        this.priceHoursCust = priceHoursCust;
        this.hoursCust = hoursCust;
        this.income = income;
        this.performer = performer;
        this.priceHoursOur = priceHoursOur;
        this.hoursOur = hoursOur;
        this.amountOur = amountOur;
        this.profit = profit;
        this.objectName = objectName;
        this.vozvrat = vozvrat;
    }

    public Date getDate() {
        return date;
    }

    public String getChIN() {
        return chIN;
    }

    public String getChOut() {
        return chOut;
    }

    public String getTecName() {
        return tecName;
    }

    public String getManagerName() {
        return managerName;
    }

    public String getCustomer() {
        return customer;
    }

    public String getPriceHoursCust() {
        return priceHoursCust;
    }

    public String getHoursCust() {
        return hoursCust;
    }

    public String getIncome() {
        return income;
    }

    public String getPerformer() {
        return performer;
    }

    public String getPriceHoursOur() {
        return priceHoursOur;
    }

    public String getHoursOur() {
        return hoursOur;
    }

    public String getAmountOur() {
        return amountOur;
    }

    public String getProfit() {
        return profit;
    }

    public String getObjectName() {
        return objectName;
    }

    public String getVozvrat() {
        return vozvrat;
    }

    public String getComment() {
        return comment;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public void setChIN(String chIN) {
        this.chIN = chIN;
    }

    public void setChOut(String chOut) {
        this.chOut = chOut;
    }

    public void setTecName(String tecName) {
        this.tecName = tecName;
    }

    public void setManagerName(String managerName) {
        this.managerName = managerName;
    }

    public void setCustomer(String customer) {
        this.customer = customer;
    }

    public void setPriceHoursCust(String priceHoursCust) {
        this.priceHoursCust = priceHoursCust;
    }

    public void setHoursCust(String hoursCust) {
        this.hoursCust = hoursCust;
    }

    public void setIncome(String income) {
        this.income = income;
    }

    public void setPerformer(String performer) {
        this.performer = performer;
    }

    public void setPriceHoursOur(String priceHoursOur) {
        this.priceHoursOur = priceHoursOur;
    }

    public void setHoursOur(String hoursOur) {
        this.hoursOur = hoursOur;
    }

    public void setAmountOur(String amountOur) {
        this.amountOur = amountOur;
    }

    public void setProfit(String profit) {
        this.profit = profit;
    }

    public void setObjectName(String objectName) {
        this.objectName = objectName;
    }

    public void setVozvrat(String vozvrat) {
        this.vozvrat = vozvrat;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public void setRowInFile(Integer rowInFile) {
        this.rowInFile = rowInFile;
    }

    public Integer getRowInFile() {

        return rowInFile;
    }

    public boolean isEmpty() {
        if (!chIN.isEmpty() || !chOut.isEmpty() || !tecName.isEmpty() || (!managerName.isEmpty() && !managerName.equals("-")) || !customer.isEmpty() || (!priceHoursCust.isEmpty() && !priceHoursCust.equals("0.0")) || !hoursCust.isEmpty() || (!income.isEmpty() && !income.equals("0.0")) || !performer.isEmpty() || (!priceHoursOur.isEmpty() && !priceHoursOur.equals("0.0")) || !hoursOur.isEmpty() || (!amountOur.isEmpty() && !amountOur.equals("0.0")) || (!profit.isEmpty() && !profit.equals("0.0")) || !objectName.isEmpty() || !vozvrat.isEmpty()) {
            return false;
        } else {
            return true;
        }
    }

    public Object[] toRow() {
        return new Object[]{(new SimpleDateFormat("dd.MM.yyyy")).format(date.getTime()), chIN, chOut, tecName, managerName, customer, priceHoursCust, hoursCust, income, performer, priceHoursOur, hoursOur, amountOur, profit, objectName, vozvrat, comment.equals("") ? "" : "*"};
    }
}
